﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.BLL;
using Grove.ORM;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;

public partial class Bidder_RemoteDeciphering : System.Web.UI.Page
{
    string Pwd, RPrice;
    Int32 prjid = 1;
    public String strConditions;
    Int64 Account_TBR;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                String SQL;
                prjid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));
                Account_TBR = SysFun.ToInt(_Account.AccountId);
                if (Pricedecipheringswitch())
                {
                    Db db = new Db();
                    System.Data.IDataReader dr;
                    SQL = "Select BidSuccess,memo2 From ProjectCompanyAttribution Where  accountid =" + Account_TBR + " and projectid = " + prjid;
                    dr = db.DataReader(SQL);
                    while (dr.Read())
                    {
                        if ((SysFun.ToInt(dr["BidSuccess"].ToString()) == 1) && (SysFun.ToInt(dr["memo2"].ToString()) == 1))
                        {
                            showGridView();
                            this.LBPwd.Visible = false;
                            this.LBRpwd.Visible = false;
                            this.PricePassword.Visible = false;
                            this.RetinuePassword.Visible = false;
                            Button1.Visible = false;
                            Button2.Visible = false;
                        }
                    }
                }
                else
                {
                    Response.Write("<script language=JavaScript>history.go(-1);</script>");
                }
            }

        }
    }
    /// <summary>
    /// 报价解密
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {

        string strSql,strSql2;
        Pwd = PricePassword.Text.Trim();
        RPrice = RetinuePassword.Text.Trim();
        int number = 0;
        //Label3.Visible = false;

        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            Account_TBR = SysFun.ToInt(_Account.AccountId);
            if (PricePassword.Text.Trim() == "" || RetinuePassword.Text.Trim() == "")
            {
                Response.Write("<script language=JavaScript>alert('请填写密码');</script>");
            }
            else
            {

                Db db = new Db();
                System.Data.IDataReader dr;
                strSql = "select count(*) as cun from ProjectCompanyAttribution where PricePassword='" + Md5check.MD5(Pwd) + "' and RetinuePassword='" + Md5check.MD5(RPrice) + "' and projectid = " + prjid + " and accountid=" + Account_TBR;
                dr = db.DataReader(strSql);
                while (dr.Read())
                {
                    number = Convert.ToInt32(dr["cun"].ToString());
                }
                dr.Close();
                dr.Dispose();

                if (number == 0)
                {
                    Response.Write("<script language=javaScript>window.alert('密码错误!');</script>");
                }
                else
                {
                    //if (Config.EnableEncrypt && !HZ.Encrypt.EncryptWrapper.SetKey)
                    //{
                    //    HZ.Web.MessageBox.Show("未加载密钥，无法解密！！");
                    //}
                    strSql = "update ProjectCompanyAttribution set BidSuccess=1,BidDate= '" + DateTime.Now + "' where accountid =" + Account_TBR + " and projectid = " + prjid;
                    strSql2 = " update BidGoods_View set SPPrice=EncryptedPrice,SMPrice=CONVERT(decimal(18,4),isnull(EncryptedPrice,0)/convert(decimal(18,4),factor)) where AccountId_TB= " + Account_TBR;
                    db.Command(strSql);
                    db.Command(strSql2);
                    // HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", strSql, "../Bidder/RemoteDeciphering.aspx");
                    if (Config.EnableEncrypt)
                    {
                        DecryptByAccount.DecByAccount(_Account.AccountId.ToString());
                    } // Add By Zeus2
                    Response.Write("<script language=javascript>alert('解密成功')</script>");
                    this.LBPwd.Visible = false;
                    this.LBRpwd.Visible = false;
                    this.PricePassword.Visible = false;
                    this.RetinuePassword.Visible = false;
                    Button1.Visible = false;
                    Button2.Visible = false;
                    //Label3.Visible = true;
                    showGridView();


                }

                db.Close();



            }
        }


    }
    /// <summary>
    /// 查询总共有多少页
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    /// <param name="PageIndex">转向第几页</param>
    public void showGridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "BidGoods_View";                                               //要查询的表明
        dbPage.Key = "Bidid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "Bidid,ProductName,Medicinemodel,Outlookc,Unit,FactoryName,Goodsid,GoodsName,salername,factor,FactoryUserCode,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') as pack1,smprice,spprice,case BidGoods_View.QualityLevels when '0' then '未审核' when '1' then '审核通过' when '2' then '审核未通过' end as CheckStatus,nopass,case  when Memo3='0' then '已保存未提交' when Memo3 is null then '未保存'  when Memo3='1' then '已提交'  when Memo3='2' then '已修改'  else '未知' end memo3,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END as QualityLevels";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 50;                                                    //没有显示记录条数   
        dbPage.OrderBy = "ProductName,Medicinemodel,Outlookc,Bidid";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }


    public string fctConditions()
    {
        string strtemp;
        strtemp = " ProjectId=" + prjid.ToString() + " and  Accountid_TB=" + Account_TBR + " and CheckStatus='1' And";
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.PricePassword.Text = "";
        this.RetinuePassword.Text = "";

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //e.Row.Cells[13].Visible = false;

    }

    private bool Pricedecipheringswitch()
    {
        Db db = new Db();
        System.Data.IDataReader dr;
        string strSql = "select count(*) as cun from ProjectCompanyAttribution where memo2='1' and  projectid=" + prjid;//memo2报价解密开关，memo2＝1 开 ；0 or null 是关
        dr = db.DataReader(strSql);
        while (dr.Read())
        {
            if (SysFun.ToInt(dr["cun"].ToString()) == 0)
            {
                Response.Write("<script language=javascript>alert('解密开关未开启或已关闭！')</script>");
                dr.Close();
                dr.Dispose();
                return false;
            }

        }
        dr.Close();
        dr.Dispose();
        return true;
    }
}
